package com.atguigu.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/api/payment/alipay")
public class AlipayController {
    @Autowired
    private AlipayService alipayService;
    @Autowired
    private PaymentService paymentService;

    //统一收单下单并支付接口
    @GetMapping("/submit/{orderId}")
    @ResponseBody
    public String submit(@PathVariable Long orderId) {
        return alipayService.submit(orderId);
    }

    //回调的地址设置在了请求参数中
    //同步回调 经过网关(对外)
    @GetMapping("/callback/return")
    public String callbackReturn() {
        return "redirect:" + AlipayConfig.return_order_url;
    }

    //异步回调
    @PostMapping("/callback/notify")
    public String callbackNotify(@RequestParam Map<String, String> paramMap) {
        //验签  支付宝使用私钥签名 所以回调时需要支付宝公钥认证
        try {
            boolean signVerified = AlipaySignature.rsaCheckV1(paramMap, AlipayConfig.alipay_public_key,
                    AlipayConfig.charset, AlipayConfig.sign_type);
            if (signVerified) {
                //验签成功
                //对用户支付的金额是否正确进行二次校验
                System.out.println("总金额" + paramMap.get("total_amount"));
                //订单后续处理(重要) 不能回滚
                //1.交易是否成功
                String trade_status = paramMap.get("trade_status");
                if ("TRADE_SUCCESS".equals(trade_status)) {
                    //2.金额是否正确
                    PaymentInfo paymentInfo = paymentService.selectPayment(paramMap.get("out_trade_no"));
                    System.out.println(paymentInfo.getTotalAmount().toString());
                    String total_amount = paramMap.get("total_amount");
                    if (paymentInfo.getTotalAmount().toString().equals(total_amount)) {
                        //DB总金额与回调参数中的总金额相等
                        System.out.println("金额正确!");
                        //支付表 更新支付表
                        paymentService.updatePaymentInfo(paramMap);
                        return "success";
                    } else {
                        System.out.println("金额不正确!");
                        return "failure";
                    }
                } else {
                    System.out.println("交易不成功!");
                    return "failure";
                }
            } else {
                System.out.println("签名认证失败!");
                return "failure";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "failure";
    }

    //退款
    @GetMapping("/refund/{outTradeNo}")
    @ResponseBody
    public Result refund(@PathVariable String outTradeNo) {
        alipayService.refund(outTradeNo);
        return Result.ok();
    }
}
